home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 22 xml and adonet / xmldemo / readerwriterform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  8.3 KB  |  214 lines

  1. Imports System.Xml
  2.  
  3. Public Class ReaderWriterForm
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.  
  28.     'Required by the Windows Form Designer
  29.     Private components As System.ComponentModel.IContainer
  30.  
  31.     'NOTE: The following procedure is required by the Windows Form Designer
  32.     'It can be modified using the Windows Form Designer.  
  33.     'Do not modify it using the code editor.
  34.     Friend WithEvents txtOut As System.Windows.Forms.TextBox
  35.     Friend WithEvents btnReader As System.Windows.Forms.Button
  36.     Friend WithEvents btnReader2 As System.Windows.Forms.Button
  37.     Friend WithEvents btnWriter As System.Windows.Forms.Button
  38.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  39.         Me.btnReader = New System.Windows.Forms.Button()
  40.         Me.btnReader2 = New System.Windows.Forms.Button()
  41.         Me.txtOut = New System.Windows.Forms.TextBox()
  42.         Me.btnWriter = New System.Windows.Forms.Button()
  43.         Me.SuspendLayout()
  44.         '
  45.         'btnReader
  46.         '
  47.         Me.btnReader.Location = New System.Drawing.Point(8, 24)
  48.         Me.btnReader.Name = "btnReader"
  49.         Me.btnReader.Size = New System.Drawing.Size(120, 40)
  50.         Me.btnReader.TabIndex = 0
  51.         Me.btnReader.Text = "XmlTextReader"
  52.         '
  53.         'btnReader2
  54.         '
  55.         Me.btnReader2.Location = New System.Drawing.Point(8, 72)
  56.         Me.btnReader2.Name = "btnReader2"
  57.         Me.btnReader2.Size = New System.Drawing.Size(120, 40)
  58.         Me.btnReader2.TabIndex = 1
  59.         Me.btnReader2.Text = "XmlTextReader 2"
  60.         '
  61.         'txtOut
  62.         '
  63.         Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  64.                     Or System.Windows.Forms.AnchorStyles.Left) _
  65.                     Or System.Windows.Forms.AnchorStyles.Right)
  66.         Me.txtOut.Location = New System.Drawing.Point(144, 16)
  67.         Me.txtOut.Multiline = True
  68.         Me.txtOut.Name = "txtOut"
  69.         Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
  70.         Me.txtOut.Size = New System.Drawing.Size(368, 288)
  71.         Me.txtOut.TabIndex = 2
  72.         Me.txtOut.Text = ""
  73.         Me.txtOut.WordWrap = False
  74.         '
  75.         'btnWriter
  76.         '
  77.         Me.btnWriter.Location = New System.Drawing.Point(8, 120)
  78.         Me.btnWriter.Name = "btnWriter"
  79.         Me.btnWriter.Size = New System.Drawing.Size(120, 40)
  80.         Me.btnWriter.TabIndex = 3
  81.         Me.btnWriter.Text = "XmlTextWriter"
  82.         '
  83.         'Form1
  84.         '
  85.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  86.         Me.ClientSize = New System.Drawing.Size(528, 317)
  87.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWriter, Me.txtOut, Me.btnReader2, Me.btnReader})
  88.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  89.         Me.Name = "Form1"
  90.         Me.Text = "XML Demo"
  91.         Me.ResumeLayout(False)
  92.  
  93.     End Sub
  94.  
  95. #End Region
  96.  
  97.     Sub OutText(ByVal s As String)
  98.         txtOut.AppendText(s)
  99.         txtOut.AppendText(ControlChars.CrLf)
  100.     End Sub
  101.  
  102.     ' create an XmlReader
  103.  
  104.     Private Sub btnReader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReader.Click
  105.         txtOut.Clear()
  106.         Dim xtr As New XmlTextReader("pubs.xml")
  107.  
  108.         Do While xtr.Read
  109.             Select Case xtr.NodeType
  110.                 Case XmlNodeType.Document
  111.                     ' The root element in the XML data
  112.                 Case XmlNodeType.Element
  113.                     ' An XML element
  114.                     If xtr.Name = "pub_name" Then
  115.                         xtr.Read()
  116.                         OutText(xtr.Value)
  117.                     End If
  118.                 Case XmlNodeType.EndElement
  119.                     ' A closing XML tag
  120.                 Case XmlNodeType.Attribute
  121.                     ' An attribute
  122.                 Case XmlNodeType.Text
  123.                     ' Text value
  124.                 Case XmlNodeType.CDATA
  125.                     ' A CDATA section
  126.             End Select
  127.         Loop
  128.         xtr.Close()
  129.     End Sub
  130.  
  131.     Private Sub btnReader2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReader2.Click
  132.         txtOut.Clear()
  133.  
  134.         Dim xtr As New XmlTextReader("invoices.xml")
  135.         Do While xtr.Read
  136.             If xtr.NodeType = XmlNodeType.Element Then
  137.                 ' Display the name of current node.
  138.                 txtOut.AppendText("<" & xtr.Name)
  139.                 ' Display name and value of attributes, if any.
  140.                 If xtr.HasAttributes Then
  141.                     Do While xtr.MoveToNextAttribute()
  142.                         txtOut.AppendText(" " & xtr.Name & "='" & xtr.Value & "'")
  143.                     Loop
  144.                 End If
  145.                 ' Go back to the main element.
  146.                 xtr.MoveToElement()
  147.                 ' Print the ending / if this is an empty element.
  148.                 If xtr.IsEmptyElement Then txtOut.AppendText("/")
  149.                 ' Close the tag.
  150.                 OutText(">")
  151.             End If
  152.         Loop
  153.         xtr.Close()
  154.     End Sub
  155.  
  156.     ' Convert a semicolon-delimited text file into XML.
  157.  
  158.     Private Sub btnWriter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriter.Click
  159.  
  160.         ' Open the file, read its contents.
  161.         Dim sr As New System.IO.StreamReader("employees.dat")
  162.         Dim fileText As String = sr.ReadToEnd
  163.         sr.Close()
  164.  
  165.         ' Create the output XML file.
  166.         Dim xtw As New XmlTextWriter("employees.xml", System.Text.Encoding.UTF8)
  167.         ' Indent tags by 2 characters.
  168.         xtw.Formatting = Formatting.Indented
  169.         xtw.Indentation = 2
  170.         ' Enclose attributes' values in double quotes.
  171.         xtw.QuoteChar = """"c
  172.         ' Create the following XML declaration for this XML document:
  173.         '    <?xml version="1.0" standalone="yes" ?>
  174.         xtw.WriteStartDocument(True)
  175.         ' Add a comment.
  176.         xtw.WriteComment("Data converted from employees.dat file")
  177.         ' The root element is <Employees>.
  178.         xtw.WriteStartElement("Employees")
  179.  
  180.         ' This regular expression defines a row of elements, and assigns a name
  181.         ' to each group (that is, a field in the text row).
  182.         Dim re As New System.Text.RegularExpressions.Regex("""(?<fname>[^""]+)"";""(?<lname>[^""]+)"";(?<bdate>[^;]+);""(?<addr>[^""]+)"";""(?<city>[^""]+)""")
  183.         Dim ma As System.Text.RegularExpressions.Match
  184.  
  185.         ' This variable will provide a unique id for each employee.
  186.         Dim id As Integer
  187.  
  188.         For Each ma In re.Matches(fileText)
  189.             ' A new line has been found, increment employee ID.
  190.             id += 1
  191.             ' write a new <Employee id="xxx"> element.
  192.             xtw.WriteStartElement("Employee")
  193.             xtw.WriteAttributeString("id", id.ToString)
  194.             ' write fields as nested elements containing text.
  195.             xtw.WriteElementString("firstName", ma.Groups("fname").Value)
  196.             xtw.WriteElementString("lastName", ma.Groups("lname").Value)
  197.             xtw.WriteElementString("birthDate", ma.Groups("bdate").Value)
  198.             xtw.WriteElementString("address", ma.Groups("addr").Value)
  199.             xtw.WriteElementString("city", ma.Groups("city").Value)
  200.             ' Close the <Employee> element.
  201.             xtw.WriteEndElement()
  202.         Next
  203.         ' close the root element (and all pending elements, if any)
  204.         xtw.WriteEndDocument()
  205.         xtw.Close()
  206.  
  207.         ' Read the file into the txtOut control.
  208.         Dim sr2 As New System.IO.StreamReader("employees.xml")
  209.         txtOut.Text = sr2.ReadToEnd
  210.         sr2.Close()
  211.  
  212.     End Sub
  213. End Class
  214.